package com.fowo.api.mp.controller;

import com.fowo.api.common.model.R;
import com.fowo.api.mp.model.CheckSignatureRequest;
import com.fowo.api.mp.model.JsApiConfigResult;
import com.fowo.api.mp.model.WxUserInfo;
import com.fowo.api.mp.service.OfficialAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Tag(name = "公众号")
@RequestMapping("/api/oa")
@RestController
public class OfficialAccountController {

    @Resource
    private OfficialAccountService officialAccountService;

    @Operation(summary = "响应微信发送的 Token 验证")
    @GetMapping("checkSignature")
    public void checkSignature(CheckSignatureRequest request, HttpServletResponse response) throws Exception {
        response.getWriter().write(officialAccountService.checkSignature(request));
    }

    @Operation(summary = "获取网页授权的跳转地址")
    @GetMapping("authorize-url")
    public R<String> getAuthorizeUrl(
            @RequestParam String redirectUrl,
            @RequestParam(required = false) String state,
            @RequestParam(required = false, defaultValue = "true") boolean userInfoScope
    ) {
        return R.data(officialAccountService.getAuthorizeUrl(redirectUrl, state, userInfoScope));
    }

    @Operation(summary = "网页授权 code 换取微信用户信息(非系统用户)")
    @GetMapping("wx-user")
    public R<WxUserInfo> getUserInfo(@RequestParam String code) {
        return R.data(() -> officialAccountService.getUserInfoByCode(code));
    }

    @Operation(summary = "获取 jssdk 权限验证配置")
    @GetMapping("wx-config")
    public R<JsApiConfigResult> getJsApiConfig(String url) {
        return R.data(() -> officialAccountService.getJsApiConfig(url));
    }
}
